package com.atguigu.gmall.product.init;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName: InitRunner
 * @Author: WDD
 * @CreateTime:
 * @Description: product启动后自动进行sku位面占位操作
 */
@Component
public class InitRunner implements CommandLineRunner {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    SkuInfoService skuInfoService;

    //位面占位
    public void initBitMap(){
        //获取skuid
        List<SkuInfo> skuIdList = skuInfoService.lambdaQuery()
                .select(SkuInfo::getId)
                .list();
        //存入位面
        skuIdList.stream().forEach(item -> {
            stringRedisTemplate.opsForValue().setBit(RedisConst.SKUID_BITMAP, item.getId(), true);
        });
    }

    @Override
    public void run(String... args) throws Exception {
        initBitMap();
    }
}
